//
//  MainViewController.swift
//  sinazd
//
//  Created by apple on 16/3/6.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    
    let mainTabbar = MainTabBar()
  //TODO:这里需要完善，判断在登录的情况下才可以发布微博，没有登录 ，则切换到注册和登录界面（或者弹出一个提示：您需要登录才可以发布微博哦）
    @objc func plusBtnDidClick() {
        
        let nav = UINavigationController(rootViewController: ComposeViewController())
        
        presentViewController(nav, animated: true, completion: nil)
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        mainTabbar.plusBtn.addTarget(self, action: "plusBtnDidClick", forControlEvents: .TouchUpInside)
        
        
        setValue(mainTabbar, forKey: "tabBar")
        
       //调用函数添加所有的子视图控制器
        addChildViewControllers()
    }
       //此函数用来添加所有的子视图控制器
    private func addChildViewControllers() {
        
        addChildViewController(HomeTableViewController(), title: "首页", imagename: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imagename: "tabbar_message_center")
        addChildViewController(DiscoverTableViewController(), title: "发现", imagename: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imagename: "tabbar_profile")
        
    }
    
       //此函数是 封装一个添加子视图控制器
    private func addChildViewController(vc:UIViewController,title:String,imagename:String) {
        
        //设置每个自控制器的头部名称
        vc.title = title
        
        //设置底部的控件
        vc.tabBarItem.title = title
        vc.tabBarItem.image = UIImage(named: imagename)
        vc.tabBarItem.selectedImage = UIImage(named: imagename + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        //设置tabBar子控件的颜色
        tabBar.tintColor = UIColor.orangeColor()
        
        
        let nav = UINavigationController(rootViewController: vc)
        addChildViewController(nav)
        
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
